package com.ruohi.util.cdn;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;


@SuppressWarnings("serial")
public class CdnHttp extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request,response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");

		PrintWriter out = response.getWriter();
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String urls = request.getParameter("urls");
		String dirs = request.getParameter("dirs");
		String callurl = request.getParameter("callurl");
		String callemail = request.getParameter("callemail");
		String callacptNotice = request.getParameter("callacptNotice");
		String msg ="";
		try {
			 msg = startRefresh( username,password, urls,dirs,callurl,callemail,callacptNotice);
		} catch (Exception e) {
			msg = e.getLocalizedMessage();
		}
		out.print(msg);
		out.flush();
		out.close();
	}

	public String startRefresh(String username,String password,String urls,String dirs,String callurl,String callemail,String callacptNotice) throws ClientProtocolException, IOException{
		String responseBody  = null;
		String task = "";
		String[] urlArr = urls.split(",");
		String urlStr = "";
		if(urlArr.length>0){
			for(int i=0;i<urlArr.length;i++){
				urlStr +="\"";
				urlStr+= urlArr[i];
				urlStr +="\"";
				if(i < urlArr.length-1){
					urlStr +=",";
				}
			}
			
		}else{
			urlStr = urls;
		}

		String[] dirArr = dirs.split(",");
		String dirStr = "";
		if(dirArr.length>0){
			for(int i=0;i < dirArr.length;i++){
				dirStr +="\"";
				dirStr+= dirArr[i];
				dirStr +="\"";
				if(i < dirArr.length-1){
					dirStr +=",";
				}
			}
			
		}else{
			dirStr = dirs;
		}
		
		task = "{\"" +
				"urls\":[" + urlStr + "]," +
				
				"\"dirs\":[" + dirStr + "]," +
				
				"\"callback\":{\"url\":[\""+callurl+"\"],\"email\":[\""+callemail+"\"],\"acptNotice\":"+callacptNotice+"}" +
			"}";
		
		HttpClient httpClient = new DefaultHttpClient();
		HttpPost httpPost = new HttpPost("https://r.chinacache.com/content/refresh");
		List<NameValuePair> formparams = new ArrayList<NameValuePair>();
		formparams.add(new BasicNameValuePair("username", username));
		formparams.add(new BasicNameValuePair("password", password));
		formparams.add(new BasicNameValuePair("task", task));
		UrlEncodedFormEntity entity;
		entity = new UrlEncodedFormEntity(formparams,"UTF-8");
		httpPost.setEntity(entity);
		ResponseHandler<String> responseHandler = new BasicResponseHandler();
		responseBody = httpClient.execute(httpPost, responseHandler);
		httpClient.getConnectionManager().shutdown();
		return responseBody;
	}
	
	/*
public String startRefresh(String username,String password,String urls,String dirs,String callurl,String callemail,String callacptNotice) throws ClientProtocolException, IOException{
		String responseBody  = null;
		String task = "";
		task = "{\"" +
				"urls\":[\""+urls+"\"]," +
				
				"\"dirs\":[\""+dirs+"\"]," +
				
				"\"callback\":{\"url\":[\""+callurl+"\"],\"email\":[\""+callemail+"\"],\"acptNotice\":"+callacptNotice+"}" +
			"}";
		
		HttpClient httpClient = new DefaultHttpClient();
		HttpPost httpPost = new HttpPost("https://r.chinacache.com/content/refresh");
		List<NameValuePair> formparams = new ArrayList<NameValuePair>();
		formparams.add(new BasicNameValuePair("username", username));
		formparams.add(new BasicNameValuePair("password", password));
		formparams.add(new BasicNameValuePair("task", task));
		UrlEncodedFormEntity entity;
		entity = new UrlEncodedFormEntity(formparams,"UTF-8");
		httpPost.setEntity(entity);
		ResponseHandler<String> responseHandler = new BasicResponseHandler();
		responseBody = httpClient.execute(httpPost, responseHandler);
		httpClient.getConnectionManager().shutdown();
		return responseBody;
	}
	
	 */

}